<?php

namespace App\Http\Controllers\admin;

use App\Dao\SettingsDao;
use App\Services\Cache;
use App\Services\Code;
use Psr\SimpleCache\InvalidArgumentException;

class SettingsController extends BaseController
{

    public function settings(){
        if (isPost()){
            SettingsDao::update_settings(request()->all());
            return  Code::message('保存成功',200);
        }
        return view('admin.settings.settings',SettingsDao::buildParms());
    }

    public function fanxian(){
        if (isPost()){
            SettingsDao::update_fanxian(request()->all());
            return  Code::message('保存成功',200);
        }
        $data = settingsDao::buildParms();
        $data['timeRange'] = date('Y-m-d H:i:s',$data['users_buy_start_time']).' - '.date('Y-m-d H:i:s',$data['users_buy_end_time']);
        return view('admin.settings.fanxian',$data);
    }


    public function paihang(){
        if(getIsHasPage()){
            $datas = SettingsDao::get_paihang(request()->all());
            return  Code::message('',200,$datas);
        }
        return view('admin.settings.paihang');
    }

    /**
     * @throws InvalidArgumentException
     */
    public function paihang_se(){
        if(isPost()){
            SettingsDao::set_paihang(request()->all());
            return  Code::message();
        }
        $paihang = Cache::getCacheValue('APP_PAIHANG');

        return view('admin.settings.paihang_se',[
            'paihang'=>$paihang == '' ? '' : $paihang['data'],
            'weekdata'=>!isset($paihang['weekdata'])  ? '' : $paihang['weekdata'],
        ]);
    }

}
